YUI.add('wfutils', function (Y) {
   // Write your module code here, and make your module available on the Y
   // object if desired.
   Y.Wfutils = {
		   /*identity can be node,modelId,or model*/
		   focusEditingProject:function(identity,pos){
			   var liNode;
			   if(identity instanceof Y.Node){
				   liNode = identity;
			   }else{
				   liNode = this.getNode('li', identity);
			   }
			   
			   if(liNode){
				   	var mod = this.getNodeModel(liNode);
				   	if(!mod.canEdit()){
				   		return;
				   	}
					var table = liNode.one('> table'); 
					table.one('.project-td').addClass('editing');
					var pta = table.one('.project-textarea');
			        var sh = pta.get('scrollHeight');
			        pta.setStyle('height',sh + "px");
			        pta.focus()
					Y.Wfutils.setCursorPosition(pta,pos);  
			   }
		   },
		   focusEditingNote:function(identity,pos){
			   var liNode;
			   if(identity instanceof Y.Node){
				   liNode = identity;
			   }else{
				   liNode = this.getNode('li', identity);
			   }
			   if(liNode){
				   	var mod = this.getNodeModel(liNode);
				   	if(!mod.canEdit()){
				   		return;
				   	}
					var table = liNode.one('> table');
					table.removeClass('emptynote');
					table.one('.note-td').addClass('editing');
					var pta = table.one('.note-textarea');
			        var sh = pta.get('scrollHeight');
			        pta.setStyle('height',sh + "px");
			        pta.focus()
					Y.Wfutils.setCursorPosition(pta,pos);
			   }
		   },
			getFirstLastSonLiNode:function(liNode,pos){
				var uls = liNode.all('> ul');
				if(uls){
					uls = uls.slice(pos);
					if(uls){
						var ul = uls.item(0);
						if(ul){
							var lis = ul.get('children');
							if(lis && lis.size() > 0){
								return lis.slice(pos).item(0);
							}
						}
					}
				}
				return null;
			},
			
			getModelById:function(mid){
				var model;
				Y.some(Y.projectListList,function(list){
					model = list.getById(mid);
					if(model){
						return true; 
					}
				});
				return model;
			},
			
			getNodeModel:function(liNode){
				Y.log(liNode);
				var ids = liNode.get('id').split('--');
				return this.getModelById(ids[1]);
			},
			
			getLiOrderNum:function(liNode){
				var pn = liNode.get('parentNode');
				var i = 0;
				p.get('children').some(function(li){
					if(liNode.get('id') == li.get('id'))return true;
					i++;
				});
				return i;
			},
			
			resetOrder:function(modelList){
				var step = 131072;
				modelList.each(function(model){
					model.set('position',step).save();
					step += 131072;
				});
			},
			
			myPosition:function(modelList,preIndex,nextIndex){
				var min = modelList.item(preIndex).get('position');
				var max = modelList.item(nextIndex).get('position');
				if((max - min)>1){
					return min + (max-min)/2;
				}else{//全部重新设定position
					this.resetOrder(modelList);
					return this.myPosition(modelList, preIndex, nextIndex);
				}
			},
			getOrderbyModelId:function(pid,mid,direction){
				var list = Y.projectListList[pid];
				var index;
				var model;
				list.some(function(m,i){
					if(m.get('id') == mid){
						model = m;
						index = i;
						return true;
					}
				});
				if(model){
					var preModel;
					if(index > 0)preModel = list.item(index - 1); 
					var nextModel;
					if(index < list.size() - 1)nextModel = list.item(index + 1);
					if('next' == direction){
						if(nextModel){//在mid和nextmodel之间。
							return myPosition(list,index,index + 1);
						}else{
							return model.get('position') + 131072;
						}
					}else{
						if(preModel){//在mid和preModel之间
							return myPosition(list,index - 1 ,index);
						}else{
							if(model.get('position') > 2){
								return model.get('position')/2;
							}else{
								this.resetOrder(list);
								return 65536;
							}
						}
					}
				}else{
					return 131072;
				}
			},
			getAvailableOrderNum:function(liNode,direction){
				if(!liNode)return 131072;
				var model = this.getNodeModel(liNode);
				var modelList = Y.projectListList[model.get('dp')];
				var index = modelList.indexOf(model);
				if('next' == direction){
					var next = modelList.item(index + 1);
					if(next){//如果有下一个，那么order不能大于下一个，只能在当前order值和下一个order值之间取。
						return this.myPosition(modelList, index, index + 1);
					}else{//如果当前是最后一个，那么添加131072即可。
						return model.get('position') + 131072;
					}
				}else{//pre
					var previous = modelList.item(index - 1);
					if(previous){
						return this.myPosition(modelList, index, index -1);
					}else{
						if(model.get('position') > 2){
							return model.get('position')/2;
						}else{
							this.resetOrder(modelList);
							return 65536;
						}
					}
				}
			},
			
			getNode:function(prefix,identity){
				if(arguments.length < 2)return null;
				
				if(Y.Lang.isString(identity)){
					;
				}else if(Y.Lang.isObject(identity)){
					identity = identity.get('id');
				}
				return Y.one('#' + prefix + '--' + identity);
			},
			
			getParentLi:function(liNode){//参数可以是model,必须通过ul的parent来实现。
				return liNode.ancestor('li');
			},
		    
		    liHasChildUl:function(liNode){
		    	var c = liNode.one('ul li');
		    	return c; 
		    },
			
			getCursorPosition:function(textareaNode){
				if(Y.UA.ie){
					if(document.selection){
			            var r = document.selection.createRange();
			            var dr = r.duplicate();
			            dr.moveToElementText(textareaNode.getDOMNode());
			            dr.setEndPoint('EndToEnd', r);
			            return dr.text.length - r.text.length;
//			            end = start + range.text.length;
					}
				}else{
					return textareaNode.get('selectionStart');
				}
				return 0;
			},
			
			setCursorPosition:function(textareaNode,pos){
				if(Y.UA.ie){
				    var r = document.selection.createRange();
				    r.moveToElementText(textareaNode.getDOMNode());
				    if("start" == pos){
				    	r.collapse(true);
				    	r.select();
				    }else if("end" == pos){
				    	r.collapse(false);
				    	r.select();
				    }else{}
				    
				}else{
					if(pos == "start"){
						textareaNode.set('selectionStart',0).set('selectionEnd',0);
					}else if(pos == "end"){
						var p = textareaNode.get('value').length;
						textareaNode.set('selectionStart',p).set('selectionEnd',p);
					}
					textareaNode.focus();
				}
			},
			
			cursorAtLast:function(textareaNode){
				var sp = this.getCursorPosition(textareaNode);
				var tl = textareaNode.get('value').length;
				return sp == tl;
			},
			
			cursorAtStart:function(textareaNode){
				var sp = this.getCursorPosition(textareaNode);
				return sp == 0;
			}
   };
},'0.0.1',{});